# -*- coding: utf-8 -*-
# 导入随机模块
import random
# 导入settings文件（）中的IPPOOL
from jdbooks.settings import IPPOOL #或者from .settings import IPPOOL（现在单独运行会报错）
# 导入官方文档对应的HttpProxyMiddleware
#from scrapy.contrib.downloadermiddleware.httpproxy import HttpProxyMiddleware     (错误原因：旧模块被弃用，改动了两个地方)
from scrapy.downloadermiddlewares.httpproxy import HttpProxyMiddleware

class Ipmid(HttpProxyMiddleware):
    # 初始化
    def __init__(self, ip=''):
        self.ip = ip
    # 请求处理
    def process_request(self, request, spider):
        # 先随机选择一个IP
        thisip = random.choice(IPPOOL)
        print("当前使用IP是："+ thisip["ipaddr"])
        request.meta["proxy"] = "http://"+thisip["ipaddr"]